import yaml
import os


class YmlReader:
    def __init__(self, ymlf):
        if os.path.exists(ymlf):
            self.ymlf = ymlf
        else:
            raise FileNotFoundError
        self._data = None

    @property
    def data(self):
        # 判断是否是第一次读取配置文件，若不是则无需重复读取
        if not self._data:
            with open(self.ymlf, 'rb') as f:
                self._data = list(yaml.safe_load_all(f))  # 需将生成器转化为列表
        return self._data
